<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\LoginRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Login extends Controller
{

    public function index(Request $request)
    {
        return view('admin.login.index');
    }

    public function login(LoginRequest $request)
    {
        $username = $request->post('username','');
        $password = $request->post('password','');

        $data = DB::table("users")->where(['username'=>$username])->first();
        if(!$data){
           return response()->json(['code' => 400, 'message' => "账号不正确"]);
        }
        if($data->password!==md5($password)){
            return response()->json(['code' => 400, 'message' => "密码不正确"]);
        }
        session(["username"=>$username]);
        return response()->json(['code' => 200, 'message' => "正在登录..."]);
    }

    public function logout(Request $request)
    {
        session(["username"=>null]);
        return redirect("/admin/login");
    }
}
